<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Modules\JasminumCalendar\Controllers;

use Gotcha\App\Framework\Controller;
use GotchaDatabase\JasminumCalendarEventsQuery;

class DefaultController extends Controller {
    public function generateCalendarView() {
        if(!isset($_POST['currentYearNumber'])) {
            $currentYear = date('Y');
        } else {
            $currentYear = $_POST['currentYearNumber'];
        }

        if(!isset($_POST['currentMonthNumber'])) {
            $currentMonthNumber = date('n');
        } else {
            $currentMonthNumber = $_POST['currentMonthNumber'];
        }

        // Initialize variables
        $currentMonthDaysNumber = date('t', mktime(0, 0, 0, $currentMonthNumber, 1, $currentYear));
        $currentMonthFirstDayNumberInWeek = date('N', mktime(0, 0, 0, $currentMonthNumber, 1, $currentYear));

        // Initialize database
        $eventsQuery = JasminumCalendarEventsQuery::create()
                        ->find();

        $events = $eventsQuery->getData();

        // Initialize data
        $eventsData = array();
        foreach($events as $event) {
            $eventsData[$event->getHall()][$event->getDate('Y-m-d')] = $event->getDescription();
        }

        // HTML cache
        $html = '';

        for($_i = 1, $_length = $currentMonthDaysNumber; $_i <= $_length; $_i++) {
            // If current $i is 1(first day of the month) we should
            // create blank divs in case current week doesn't start
            // on monday
            if($_i === 1) {
                for($_y = 1, $_length2 = $currentMonthFirstDayNumberInWeek - 1; $_y <= $_length2; $_y++) {
                    $html .= '<div class="blankDay"></div>';
                }
            }

            $html .= '<div class="day"><div class="dayNumber">'.$_i.'</div>';

            // Check for events existence
            $newDateFormat = \DateTime::createFromFormat('Y-n-j', $currentYear.'-'.$currentMonthNumber.'-'.$_i)->format('Y-m-d');

            for($_currentHall = 0, $_length4 = 3; $_currentHall <= $_length4; $_currentHall++) {
                if(array_key_exists($_currentHall, $eventsData)) {
                    if(array_key_exists($newDateFormat, $eventsData[$_currentHall])) {

                        $html .= '<div class="hallReserved-'.$_currentHall.'"><div style="position: relative"><div class="eventDescription">'.$eventsData[$_currentHall][$newDateFormat].'</div></div></div>';
                    } else {
                        $html .= '<div class="hallNotReserved-'.$_currentHall.'"></div>';
                    }
                } else {
                    $html .= '<div class="hallNotReserved-'.$_currentHall.'"></div>';
                }
            }

            $html .= '</div>';

            // If we reached last day of current month we have to
            // fill calendar with blanks if needed
            if($_i == $currentMonthDaysNumber) {
                for($_z = 1, $_length3 = 42 - $currentMonthDaysNumber - $_length2; $_z <= $_length3; $_z++) {
                    $html .= '<div class="blankDay"></div>';
                }
            }
        }

        return $html;
    }
}